package com.github.niefy.qrcode;// 根据 github 提供的 maven 集成方式导入 cos xml java sdk
import com.qcloud.cos.*;
import com.qcloud.cos.auth.*;
import com.qcloud.cos.exception.*;
import com.qcloud.cos.model.*;
import com.qcloud.cos.region.*;

import java.io.File;

public class UploadDemo {
    public static void main(String[] args) throws Exception {



    }


    public static void uploadfile(String tmpSecretId, String tmpSecretKey, String sessionToken) {

        // 用户基本信息
        //String tmpSecretId = "AKIDPxJSu2tayox8hGlEcsQot7JqyQmlpnaCHLpyhGncisN5Qq6lW2i4W5hdRYsh8hTE";   // 替换为 STS 接口返回给您的临时 SecretId
        //String tmpSecretKey = "eL36/q5NRgbp0JsW/HdSGfumh2P6qeh6x8S85clEntU=";  // 替换为 STS 接口返回给您的临时 SecretKey
        //String sessionToken = "CX7Utb4xf3jxk92G2wArj1A75Ms9TiYa55fc61f8193dee0046425642e7f49081tcJkGsO-hlBvL47uRwHjqBR2McoYaRgND2SryARRzC5ySepuxsWxGvs6Jf1HIXhPGLzRZ5fpdMvHFhNKnWMtJ6NnOtgUmICEBMTrGKh6Xr5vfjvL_zhR12tieGuh728ASik72jy7S1g9YpA1HqnJBLp5Sx9zV4atjCtjQQh8sgVLSn8IOuisaWIxnONPSbu3Imama-Lqje1gWlAUVrcWWzIavJB2JUFRcxmJvvSptP13jfceGDshFzCYI3rkGHMvhx-Qe1n5SGY96gI9X_ETpKoFurRws53b5r16_T9M873iznK8WxQ6jjNnrVIhPSwBSPVfPO3LuHBfXZLCIxf5lsk9AnzPZThyXue3i7IJl9YGe13OuUaZEbDMz0qe-rYfu-mKu8fjMexs_gpi0cw4VyqPA7apNlE1HDNG1_z-AfYTpZPAuwIRJcnpLb0xh7cO31Al31eR7dQB9Ii8CkCB2NnIKcIjokAZB3VO2yrtuQH7BCPUEe0xXtRAfglRESLR2d2HYkHmvwPPl2A1eqAh9ixJSvIMm1EAcf3u970rK12DzKIK0M4WYf1_Va3tz9k0vBWyhaozX8T8efJ6eJLIl17P3lhLQs64Hu59OHE8puyO4EHEGZtsi0OurlS_b6PNzyHVP_UmsXbRjppl0AXLafW9Y-62eN2PKzqFN4j1uU71dDkdA--EmH38csZu7NLX9pCWfTJG9Y43KAnJ80unarhH3-KGmvOwv18fpmd4unete4n6NlJQM8U8himl67LkZ2XtKia8Abptx5I6u4itHNDOLMOlEcJYsKR_8WLAgRBgKIkdSs3SY96cumhaOfwVC_H-KeisvZVsEuK-gl02NQ";

        // 1 初始化用户身份信息(secretId, secretKey)
        COSCredentials cred = new BasicCOSCredentials(tmpSecretId, tmpSecretKey);
        // 2 设置 bucket 区域,详情请参阅 COS 地域 https://cloud.tencent.com/document/product/436/6224
        ClientConfig clientConfig = new ClientConfig(new Region("ap-shanghai"));
        // 3 生成 cos 客户端
        COSClient cosclient = new COSClient(cred, clientConfig);
        // bucket 名需包含 appid
        String bucketName = "7072-prod-6gcztoop0371ed75-1314793026";

        String key = "exampleobject";
        // 上传 object, 建议 20M 以下的文件使用该接口
        File localFile = new File("D:/四五华庭5号楼6单元_0.png");
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);

        // 设置 x-cos-security-token header 字段
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setSecurityToken(sessionToken);
        putObjectRequest.setMetadata(objectMetadata);

        try {
            PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
            // 成功：putobjectResult 会返回文件的 etag
            String etag = putObjectResult.getETag();
        } catch (CosServiceException e) {
            //失败，抛出 CosServiceException
            e.printStackTrace();
        } catch (CosClientException e) {
            //失败，抛出 CosClientException
            e.printStackTrace();
        }

        // 关闭客户端
        cosclient.shutdown();

    }
}